using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Gadgets
{
    public partial class MDIForms : Form
    {
        private TreeMenu tree_menu = new TreeMenu();
        private ReportMenu report_menu = new ReportMenu();
        public MDIForms()
        {
            InitializeComponent();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
                // TODO: Add code here to open the file.
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: Add code here to save the current contents of the form to a file.
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard to insert the selected text or images into the clipboard
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Use System.Windows.Forms.Clipboard.GetText() or System.Windows.Forms.GetData to retrieve information from the clipboard.
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void MDIForms_Load(object sender, EventArgs e)
        {
            try
            {
                tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
                report_menu.Show(dockPanel1, DockState.DockRightAutoHide);
                InitData();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login login = new Login();
            try
            {
                if (login.ShowDialog() == DialogResult.OK)
                {
                    changePasswordToolStripMenuItem.Enabled = true;

                    tree_menu.Restart();
                    report_menu.Restart();
                    toolStripStatusUsername.Text = DeclareLibrary.USER_NAME;
                    toolStripStatusMarketDate.Text = DeclareLibrary.MARKET_DAY.ToString("dd/MM/yyyy");                    
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void InitData()
        {
            DataSet dsParameters = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC009004,
                new object[0] { });
            if (dsParameters.Tables[0].Rows.Count > 0)
            {
                DataRow r = dsParameters.Tables[0].Rows[0];

                DeclareLibrary.MEMBER_CODE = r[0].ToString();
                if (ABSCommonLibrary.GetSHA1HashData(DeclareLibrary.MEMBER_CODE) !=
                    r[16].ToString())
                {
                    ABSCommonLibrary.ShowMessage("He thong chua duoc dang ky thanh vien, lien he voi hoanvk");
                    loginToolStripMenuItem.Enabled = false;
                    return;
                }
                DeclareLibrary.ADDRESS = r[3].ToString();
                DeclareLibrary.WEBSITE = r[4].ToString();
                DeclareLibrary.COMPANY = r[1].ToString();
                DeclareLibrary.MARKET_DAY = Convert.ToDateTime(r[11]);
                DeclareLibrary.SETTLE_DATE = Convert.ToDateTime(r[12]);
                DeclareLibrary.BROKER_RATE = Convert.ToDouble(r[9]);
                DeclareLibrary.ADVANCE_RATE = Convert.ToDouble(r[10]);
                DeclareLibrary.PORTFOLIO_ACCOUNT = r[14].ToString();
                DeclareLibrary.GENERAL_ACCOUNT = r[6].ToString();

                DeclareLibrary.PCFLAG_DEFAULT = r[13].ToString();
                DeclareLibrary.SYSTEM_STATUS = r[15].ToString();

                DeclareLibrary.XML_DATA = ABSCommonLibrary.GetXmlData();
                this.toolStripStatusFirm.Text = DeclareLibrary.MEMBER_CODE;
                this.Text = DeclareLibrary.COMPANY + " - " + DeclareLibrary.ADDRESS;
                if (DeclareLibrary.SYSTEM_STATUS == "N")
                {
                    ABSCommonLibrary.ShowMessage("He thong chua duoc bat dau ngay giao dich hoac dang la ngay nghi");
                    loginToolStripMenuItem.Enabled = false;
                }
            }
            else
            {
                ABSCommonLibrary.ShowMessage("He thong chua duoc dang ky thanh vien, lien he voi hoanvk");
                loginToolStripMenuItem.Enabled = false;
            }
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart(); 
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePassword obj = new ChangePassword();
            obj.ShowDialog();
        }

        private void toolStripMenuItemFunction_Click(object sender, EventArgs e)
        {
            if (tree_menu.DockState == DockState.Hidden)
                //tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
                tree_menu.DockState = tree_menu.VisibleState;

            tree_menu.Activate();
        }

        private void toolStripMenuItemReport_Click(object sender, EventArgs e)
        {
            if (report_menu.DockState == DockState.Hidden)
                //tree_menu.Show(dockPanel1, DockState.DockLeftAutoHide);
                report_menu.DockState = report_menu.VisibleState;

            report_menu.Activate();
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F10}");
        }

        private void searchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F9}");
        }

        private void commitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SendKeys.Send("{F11}");                
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }            
        }

        private void toolStripButtonSearch_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F9}");
        }

        private void toolStripButtonCommit_Click(object sender, EventArgs e)
        {
            try
            {
                SendKeys.Send("{F11}");                
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F10}");
        }
    }
}
